package com.lmk.qs.jdbc;

import com.lmk.qs.domain.Emp;
import com.lmk.qs.utils.DruidUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Spring JDBC 连接池增删改查功能
 *
 * @author 青笙
 * @email 1460961774@qq.com
 */

public class SpringJDBC {
    public static void main(String[] args) {
        JdbcTemplate jp = new JdbcTemplate(DruidUtil.getDataSource());
        //增加一条数据
        String insert = "insert  into dept values(?,?)";
        //删除一条数据
        String delete = "delete  from emp where id=?";
        //更新一条数据
        String update = "update emp set name=? where id=?";
        //封装map对象
        String sql = "select * from emp where  id=? or id=?";

        String SQL2 = "select * from emp";

        List<Emp> query = jp.query(SQL2, new RowMapper<Emp>() {
            @Override
            public Emp mapRow(ResultSet resultSet, int i) throws SQLException {
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String gender = resultSet.getString(3);
                int salary = resultSet.getInt(4);
                int jobdate = resultSet.getInt(5);
                int dep_id = resultSet.getInt(6);
                Emp emp = new Emp();
                emp.setId(id);
                emp.setName(name);
                emp.setGender(gender);
                emp.setSalary(salary);
                emp.setJob_date(jobdate);
                emp.setDep_id(dep_id);

                return emp;
            }
        });
        List<Emp> query1 = jp.query(SQL2, new BeanPropertyRowMapper<Emp>(Emp.class));
//        System.out.println(query1);
//        System.out.println(query);
        Map<String, Object> map = jp.queryForMap(sql, 2, 3);
        List<Map<String, Object>> maps = jp.queryForList(SQL2);
//        for (Map<String, Object> mapss:maps
//             ) {
//            System.out.println(mapss);
//        }
//        System.out.println(maps);
//
//
//        System.out.println(map);
        int i = jp.update(update, "李四", 5);
        String sql1 = "select count(id) from emp";
        Object o = jp.queryForObject(sql1, Object.class);

        System.out.println(o);
    }
}

